package com.utn.bondisutn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Horarios extends Activity {
	
	private String[] par14 = {"Centro de transferencia","Hospital Pasteur","San Luis y Perazolo", "UTN", "Mendoza y Porfirio Seppey"};
	private String[] par16 = {"Centro de transferencia","Ituzaingo y Sarmiento", "UTN", "Lacar y Aconcagua", "Ituzaingo y Sarmiento."};
	private String[] par16B = {"Centro de transferencia","Ituzaingo y Sarmiento","UTN", "Lacar y Aconcagua", "Ituzaingo y Sarmiento."};
	
	private ArrayList<String> paradas14 = new ArrayList<String>(Arrays.asList(par14));
	private ArrayList<String> paradas16 = new ArrayList<String>(Arrays.asList(par16));
	private ArrayList<String> paradas16B = new ArrayList<String>(Arrays.asList(par16B));
	
	private final int[][] HORARIOS_14 = {{600,640,720,800,840,920,1000,1040,1120,1200,1240,1320,1400,1440,1520,1600,1640,1720,1800,1840,1920,2000,2040,2120,2200},
			{606,646,726,806,846,926,1006,1046,1126,1206,1246,1326,1406,1446,1526,1606,1646,1726,1806,1846,1926,2006,2046,2126,2206},
			{615,655,735,815,855,935,1015,1055,1135,1215,1255,1335,1415,1455,1535,1615,1655,1735,1815,1855,1935,2015,2055,2135,2215},
			{620,700,740,820,900,940,1020,1100,1140,1220,1300,1340,1420,1500,1540,1620,1700,1740,1820,1900,1940,2020,2100,2140,2220},
			{630,710,750,830,910,950,1030,1110,1150,1230,1310,1350,1430,1510,1550,1630,1710,1750,1830,1910,1950,2030,2110,2150,2230}};
	
	private final int[][] HORARIOS_16 = {{620,700,740,820,900,940,1020,1100,1140,1220,1300,1340,1420,1500,1540,1620,1700,1740,1820,1900,1940,2020,2100,2140},
			{627,707,747,827,907,947,1027,1107,1147,1227,1307,1347,1427,1507,1547,1627,1707,1747,1827,1907,1947,2027,2107,2147},
			{632,712,752,832,912,952,1032,1112,1152,1232,1312,1352,1432,1512,1552,1632,1712,1752,1832,1912,1952,2032,2112,2152},
			{637,717,757,837,917,957,1037,1117,1157,1237,1317,1357,1437,1517,1557,1637,1717,1757,1837,1917,1957,2037,2117,2157},
			{650,730,810,850,930,1010,1050,1130,1210,1250,1330,1410,1450,1530,1610,1650,1730,1810,1850,1930,2010,2050,2130,2210}};
	
	private final int[][] HORARIOS_16_B = {{600,640,720,800,840,920,1000,1040,1120,1200,1240,1320,1400,1440,1520,1600,1640,1720,1800,1840,1920,2000,2040,2120},
			{607,647,727,807,847,927,1007,1047,1127,1207,1247,1327,1407,1447,1527,1607,1647,1727,1807,1847,1927,2007,2047,2127},
			{612,652,732,812,852,932,1012,1052,1132,1212,1252,1332,1412,1452,1532,1612,1652,1732,1812,1852,1932,2012,2052,2132},
			{617,657,737,817,857,937,1017,1057,1137,1217,1257,1337,1417,1457,1537,1617,1657,1737,1817,1857,1937,2017,2057,2137},
			{630,710,750,830,910,950,1030,1110,1150,1230,1310,1350,1430,1510,1550,1630,1710,1750,1830,1910,1950,2030,2110,2150}};

	private ListView listView;
	private Calculador calc;
	private ArrayList<Horario> horariosLst;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_horarios);
		
		int tipoConsulta = getIntent().getExtras().getInt("tipo");
		String parada = getIntent().getExtras().getString("parada");
		
		TextView subTitle = (TextView) findViewById(R.id.horarioTxt);
		listView = (ListView) findViewById(R.id.horariosLst);
		
		if (tipoConsulta==EnDondeEstas.IR_A_LA_UTN){
			subTitle.setText(parada + " a UTN"); 
			verHorarios(parada,parada);
		} else {
			subTitle.setText("UTN a " + parada);
			verHorarios(parada,"UTN");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.horarios, menu);
		return true;
	}
	
	private void verHorarios(String paradaPasaPor, String paradaConsulta){
		calc = new Calculador(Calendar.getInstance());
		horariosLst = new ArrayList<Horario>();
		
		cargarHorario(paradas14, HORARIOS_14, R.drawable.linea_14, paradaPasaPor, paradaConsulta);
		cargarHorario(paradas16, HORARIOS_16, R.drawable.linea_16, paradaPasaPor, paradaConsulta);
		cargarHorario(paradas16B, HORARIOS_16_B, R.drawable.linea_16_b, paradaPasaPor, paradaConsulta);
		
		listView.setAdapter(new HorarioItemAdapter(this, android.R.layout.simple_list_item_1, horariosLst));
	}
	
	private void cargarHorario(ArrayList<String> paradasDeLinea, int[][] horariosLinea,
			int iconoLinea, String paradaPasaPor, String paradaConsulta){
		if (paradasDeLinea.contains(paradaPasaPor)){
			calc.calcularProximos(horariosLinea, paradasDeLinea.indexOf(paradaConsulta));
			horariosLst.add(new Horario(iconoLinea, calc.getProximo(),calc.getSiguiente()));
		}
	}
	
	public class HorarioItemAdapter extends ArrayAdapter<Horario> {

		private ArrayList<Horario> horarios;
		
		public HorarioItemAdapter(Context context, int textViewResourceId,
				ArrayList<Horario> horarios) {
			super(context, textViewResourceId, horarios);
			this.horarios = horarios;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.horario_list_item, null);
			}
			
			Horario horario = horarios.get(position);
			if (horario!=null){
				ImageView imagen = (ImageView) v.findViewById(R.id.linea);
				TextView proximo = (TextView) v.findViewById(R.id.proximoTxt);
				TextView siguiente = (TextView) v.findViewById(R.id.siguienteTxt);
				
				if (imagen != null) {
					imagen.setImageResource(horario.getLineaImagenId());
				}
				if (proximo != null) {
					proximo.setText(horario.getProximo());
				}
				if (siguiente != null) {
					siguiente.setText(horario.getSiguiente());
				}
			}
			return v;
		}

	}

}
